package com.demo.dropdataack.service.common;


import com.alibaba.fastjson.JSONObject;
import com.demo.dropdataack.entity.Token;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;


/**
 * @author mm
 */
public class TokenHelper {
    private static  String url = "https://bts.travelsky.com/gateway/login";
    private static  String username = "can";
    private static  String password = "can149;a";

    @Autowired
    private  OkHttpClient okHttpClient ;


    /**
     * 获得token
     * @return
     */
    public String   GetToken()
    {
        String token = null;
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        String jsonStr = "{\"username\":\"can\",\"password\":\"can149;a\"}";
        Request.Builder getIdRequest = new Request.Builder().url(url).post(RequestBody.create(JSON, jsonStr));
        Call call = okHttpClient.newCall(getIdRequest.build());
        Response response = null;
        try
        {
            response = call.execute();
            String data = response.body().string();
            Token tokenBean = JSONObject.parseObject(data, Token.class);
            token =tokenBean.getData().getToken();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
       return  token;
      }
}
